<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Cache;
use think\Db;
use think\Request;

/**
 * 系统配置
 * Class System
 * @package app\admin\controller
 */
class Database extends AdminBase
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index(Request $request)
    {
        $environment = $request->ip(0, true);

        if ($environment == '127.0.0.1') {
            $data[0]['file_size']   = '1550609';
            $data[0]['version']     = 'v2';
            $data[0]['file_name']   = 'chehu_v2-20171211184257.sql.gz';
            $data[0]['create_time'] = date('Y-m-d H:i:s', 1512988978);
        } else {
            $dir = '/home/backup/database';

            $handler = opendir($dir);
            while (($filename = readdir($handler)) !== false) {//务必使用!==，防止目录下出现类似文件名“0”等情况
                if ($filename != "." && $filename != "..") {
                    $files[] = $filename;
                }
            }

            closedir($handler);
            $data = [];
            //打印所有文件名
            foreach ($files as $k => $value) {
                $data[$k]['file_size']   = filesize($dir . '/' . $value);
                $data[$k]['file_name']   = $value;
                $data[$k]['version']     = 'v2';
                $data[$k]['create_time'] = date('Y-m-d H:i:s', filemtime($dir . '/' . $value));
            }

        }


        return view('index', compact('data'));

//        system("/bin/bash /home/backup/mysql_back.sh");
//        exec('/home/backup/mysql_back.sh');
    }
}